package com.zhihaoliang.bezier.bezier

import android.widget.Toast
import androidx.activity.viewModels
import com.zhihaoliang.bezier.base.BaseActivity
import com.zhihaoliang.bezier.databinding.ActivityBezierBinding

/**
 * @author 支豪亮
 * @date 2023/1/9 11:16
 * @des BezierActivity 的 View
 */
class BezierActivity : BaseActivity<ActivityBezierBinding>(), BezierCallback {
    override fun initDataBinding() = ActivityBezierBinding.inflate(layoutInflater)

    private val viewModle: BezierViewModle by viewModels()

    override fun initView() {
        super.initView()

        dataBinding.viewModel = viewModle
        dataBinding.callback = this
    }

    override fun onAddPoint() {
        if (dataBinding.bezier.addPoint()) {
            dataBinding.textview.text = String.format("%s阶贝塞尔曲线", dataBinding.bezier.orderStr)
        } else {
            Toast.makeText(this, "Add point failed.", Toast.LENGTH_LONG).show()
        }
    }

    override fun onDelPoint() {
        if (dataBinding.bezier.delPoint()) {
            dataBinding.textview.text = java.lang.String.format("%s阶贝塞尔曲线",
                                                                dataBinding.bezier.orderStr)
        } else {
            Toast.makeText(this, "Delete point failed.", Toast.LENGTH_LONG).show()
        }
    }

    override fun onStartBezier() {
        val isLoop = viewModle.isLoop.value ?: false
        dataBinding.bezier.setLoop(isLoop)

        var progress = viewModle.progress.value?:0
        if (progress == 0) {
            progress = 1
        }
        dataBinding.bezier.setRate(progress * 2)

        val isChecked = viewModle.isTangent.value?:true
        dataBinding.bezier.setTangent(isChecked)

        dataBinding.bezier.start()
    }

    override fun onStopBezier() {
        dataBinding.bezier.stop()
    }

}